class PlayerScout extends GameCharacter
{
	private var stealthSound:AudioClip;
	private var INVIS_ALPHA_VALUE:float = 0.5f;
	private var VIS_ALPHA_VALUE:float = 1.0f;

	public function PlayerScout()
	{		
		super();
		
		characterName = "Scout";
		
		//Attack
		attackRadius = 5;
		attackDamage = 15;
		actualDamage = 15;
		_attackPercent = 50.0;
		
		//Sight
		sightRadius = 11;
		moveRadius = 8;
		
		//Health Status
		maxHealth = 100;
		myHealth = maxHealth;
		
		//Skills
		_maxSkillCooldown = 2;
		
		//For diplaying mouse over
		healthPercent = 100.0f;
		healthBarLength = Screen.width/9;
		style = new GUIStyle();
	    texture = new Texture2D(1, 1);
		
		//Player type
		_playerType = PlayerType.PLAYER_SCOUT;
		
		buttonSkillTexture = Resources.Load("Buttons/stealth_buttonNormal");
	 	buttonSkillHoverTexture = Resources.Load("Buttons/stealth_buttonHover");
	 	buttonSkillPressedTexture = Resources.Load("Buttons/stealth_buttonPressed");	
	 		 	
	 	skillstyle = new GUIStyle();
		skillstyle.normal.background = buttonSkillTexture;
		skillstyle.hover.background = buttonSkillHoverTexture;
		skillstyle.active.background = buttonSkillPressedTexture;		
		
		selectedSound = Resources.Load("scoutSound");	
		stealthSound = Resources.Load("stealthSound");			
	}
	
	public function attack(target:EnemyCharacter, projectileType:int)
	{
		super.attack(target, projectileType);
		
		reveal();
		
		//calcualte hit chance
		var hitChance = calculateHitChance(target) + attackChanceModifier;
		
		//fire bullet
		var bullet = Resources.Load("Projectile_dagger_flavis");
		bullet = Instantiate(bullet);
		var projectile = bullet.GetComponent(Projectile);
		
		var chance = Random.Range(0,100);
		
		//random attack chance
		if (chance < hitChance){
			//hit
			projectile.setup(this, target, true, projectileType);
			attackChanceModifier = 0;
		}else{
			//miss
			projectile.setup(this, target, false, projectileType);
			attackChanceModifier += Mathf.Floor((chance - hitChance)/5.0);
		}
		
		Destroy(bullet,5);
		
		endTurn();
	}
	
	//OVERRIDE THIS FUNCTION IN EVERY INHERITED CLASS
	public function calculateHitChance(target:EnemyCharacter)
	{
		//currently calculating based on an average atk
		//calculate distance of enemy
		var distance = Vector3.Distance(target.getSurfaceCoord(), this.getSurfaceCoord());
		//based on the maximum range of weapon, calculate the hit chance based on the optimized range
		//close - mid range weapon
		var optimizedRangeMax = 5;
		var hitChance = 100;
		//too far
		if (distance > optimizedRangeMax)
			hitChance -= (distance - optimizedRangeMax) * 20;
		//if in cover
		if (target.isInCover())
		{
			if (!target.isFlanked(this))
				hitChance -= Obstacle.HIT_CHANCE_REDUCTION;
		}
		
		//check height advantage
		var heightDifference = target.getSurfaceCoord().y - this.getSurfaceCoord().y;
		if (heightDifference > 0)
			hitChance -= heightDifference * 10;
		else if (heightDifference < 0)
			hitChance += heightDifference * -10;
		
		//minimum 10%
		if (hitChance < 10)
			hitChance = 10;
		else if (hitChance > 100)
			hitChance = 100;
		return hitChance;
	}
	
	public function calculateActualDamage(target:EnemyCharacter)
	{
		//if in cover
		if (target.isInCover())
		{
			//check whether flanked
			if (target.isFlanked(this))
			{
				return attackDamage *2; 
			}
			else
			{
				return attackDamage;
			}
		}
		//not in cover
		else
		{
			return attackDamage *2;
		}
	}
	
	public function skill() // Scout
	{
		if(!_isCooldown){
			_visibleToEnemy = false;
			setAnimationState(SKILL_ANI);
			startCooldown();
			
			
			this.transform.Find("walk").gameObject.GetComponent(changeOpacity).setOpacity(INVIS_ALPHA_VALUE);
			this.transform.Find("jump").gameObject.GetComponent(changeOpacity).setOpacity(INVIS_ALPHA_VALUE);
			this.transform.Find("skill").gameObject.GetComponent(changeOpacity).setOpacity(INVIS_ALPHA_VALUE);
			this.transform.Find("attack").gameObject.GetComponent(changeOpacity).setOpacity(INVIS_ALPHA_VALUE);
			this.transform.Find("toCover").gameObject.GetComponent(changeOpacity).setOpacity(INVIS_ALPHA_VALUE);
			
			
			audio.PlayClipAtPoint(stealthSound,Vector3(0,0,0), 0.5f);
			
			Debug.Log("Play Disappear animation here");
			
			
			
			_attackableText = Resources.Load("Attackable_text");
			_attackableText = MonoBehaviour.Instantiate(_attackableText);
			_attackableText.transform.position = _tileReference.getSurfaceCoord();
			_attackableText.transform.position.y += 2;
			_attackableText.GetComponent(AttackableText).setAutoRemoveText("Invisible!", 35, 3, Color.white);
			
		}
	}
	
	//@Override resetTurn
	public function resetTurn(){
		super.resetTurn();
		
		reveal();			
	}
	
	private function reveal(){
		// make player visible again
		if(!_visibleToEnemy){
			Debug.Log("Play Appear animation here");
			this.transform.Find("walk").gameObject.GetComponent(changeOpacity).setOpacity(VIS_ALPHA_VALUE);
			this.transform.Find("jump").gameObject.GetComponent(changeOpacity).setOpacity(VIS_ALPHA_VALUE);
			this.transform.Find("skill").gameObject.GetComponent(changeOpacity).setOpacity(VIS_ALPHA_VALUE);
			this.transform.Find("attack").gameObject.GetComponent(changeOpacity).setOpacity(VIS_ALPHA_VALUE);
			this.transform.Find("toCover").gameObject.GetComponent(changeOpacity).setOpacity(VIS_ALPHA_VALUE);
			_attackableText = Resources.Load("Attackable_text");
			_attackableText = MonoBehaviour.Instantiate(_attackableText);
			_attackableText.transform.position = _tileReference.getSurfaceCoord();
			_attackableText.transform.position.y += 2;
			_attackableText.GetComponent(AttackableText).setAutoRemoveText("Revealed!", 35, 3, Color.white);
			
			_visibleToEnemy = true;
		}
	}
	
	
	//ANIMATION FUNCTIONS
	protected function setAnimationState(state:int)
	{
		var walk_model = this.transform.Find("walk").gameObject;
		var jump_model = this.transform.Find("jump").gameObject;
		var skill_model = this.transform.Find("skill").gameObject;
		var attack_model = this.transform.Find("attack").gameObject;
		var cover_in_model = this.transform.Find("toCover").gameObject;

		switch (state)
		{
			case STOP_ANI:
				switch (_currentAniState)
				{
					case JUMP_ANI:
						//don't stop any animation
						break;
					default:
						walk_model.animation.Stop();
						jump_model.animation.Stop();
						skill_model.animation.Stop();
						attack_model.animation.Stop();
						cover_in_model.animation.Stop();
				}
				break;
				
			case WALK_ANI:
				jump_model.SetActive(false);
				skill_model.SetActive(false);
				attack_model.SetActive(false);
				cover_in_model.SetActive(false);
				walk_model.SetActive(true);
				walk_model.animation.Play();
				break;
				
			case JUMP_ANI:
				walk_model.SetActive(false);
				skill_model.SetActive(false);
				attack_model.SetActive(false);
				cover_in_model.SetActive(false);
				jump_model.SetActive(true);
				jump_model.animation.Play();
				break;
				
			case SKILL_ANI:
				walk_model.SetActive(false);
				jump_model.SetActive(false);
				attack_model.SetActive(false);
				cover_in_model.SetActive(false);
				skill_model.SetActive(true);
				skill_model.animation.Play();
				break;
				
			case ATTACK_ANI:
				walk_model.SetActive(false);
				jump_model.SetActive(false);
				skill_model.SetActive(false);
				cover_in_model.SetActive(false);
				attack_model.SetActive(true);
				attack_model.animation.Play();
				break;
			
			case COVER_IN_ANI:
				walk_model.SetActive(false);
				jump_model.SetActive(false);
				skill_model.SetActive(false);
				attack_model.SetActive(false);
				if (_currentAniState != COVER_IN_ANI)
				{
					cover_in_model.SetActive(true);
					cover_in_model.animation.Play();
				}
				break;
		}
		
		if(_visibleToEnemy){
			walk_model.GetComponent(changeOpacity).setOpacity(VIS_ALPHA_VALUE);
			jump_model.GetComponent(changeOpacity).setOpacity(VIS_ALPHA_VALUE);
			skill_model.GetComponent(changeOpacity).setOpacity(VIS_ALPHA_VALUE);
			attack_model.GetComponent(changeOpacity).setOpacity(VIS_ALPHA_VALUE);
			cover_in_model.GetComponent(changeOpacity).setOpacity(VIS_ALPHA_VALUE);
		}
		else{
			walk_model.GetComponent(changeOpacity).setOpacity(INVIS_ALPHA_VALUE);
			jump_model.GetComponent(changeOpacity).setOpacity(INVIS_ALPHA_VALUE);
			skill_model.GetComponent(changeOpacity).setOpacity(INVIS_ALPHA_VALUE);
			attack_model.GetComponent(changeOpacity).setOpacity(INVIS_ALPHA_VALUE);
			cover_in_model.GetComponent(changeOpacity).setOpacity(INVIS_ALPHA_VALUE);
		}
		
		_currentAniState = state;
	}
	
}